package com.xps.charmingshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.utils.JUtils;
import com.xps.charmingshop.R;
import com.xps.charmingshop.bean.MenuBean;
import com.xps.charmingshop.bean.ShopBean;
import com.xps.charmingshop.view.activity.UpdateMenuActivity;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class CollocationListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private Context context;
    private ExpandableListView elv_collocation;
    private List<MenuBean.ReturnBodyBean> data;

    public CollocationListAdapter(Context context, ExpandableListView elv_collocation, List<MenuBean.ReturnBodyBean> data) {
        this.context = context;
        this.elv_collocation = elv_collocation;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getDish_info().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getDish_info().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder parentViewHolder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.collocation_list_item_parent, parent, false);
            parentViewHolder = new ParentViewHolder(convertView);
            convertView.setTag(parentViewHolder);
            AutoUtils.auto(convertView);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
        MenuBean.ReturnBodyBean collocationPackageBean = data.get(groupPosition);
        parentViewHolder.tv_collocation_name.setText(collocationPackageBean.getSort_name());
        parentViewHolder.v_space.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
        parentViewHolder.hsv_goods_list.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
        parentViewHolder.Layout_List.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        parentViewHolder.hsv_goods_list.setFocusable(false);//设置后解决套餐无法正常展开的bug
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.collocation_list_item_child, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
            AutoUtils.auto(convertView);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        final MenuBean.ReturnBodyBean.DishInfoBean collocationSkuBean = data.get(groupPosition).getDish_info().get(childPosition);
        childViewHolder.tv_goods_title.setText(collocationSkuBean.getDish_name());
        childViewHolder.tv_googs_id.setText(collocationSkuBean.getDish_id());
        childViewHolder.tv_goods_price.setText(collocationSkuBean.getDish_price());
        childViewHolder.tv_googs_sales.setText(collocationSkuBean.getDish_sales());
        dateTime(collocationSkuBean.getDish_time(), childViewHolder.tv_googs_money);
        Glide.with(context).load("http://" + collocationSkuBean.getDish_pic_src())
                .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                .skipMemoryCache(true)
                .into(childViewHolder.sdv_goods_img);
//        childViewHolder.ll_root_view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, UpdateMenuActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.putExtra("dish_id", collocationSkuBean.getDish_id());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    void dateTime(String createTime, TextView tvOrderTime) {
        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tvOrderTime.setText(simpleDateFormat.format(passDate));
    }

    /**
     * ExpandableListView 如果子条目需要响应click事件,必需返回true
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ParentViewHolder {
        private View v_space;
        private ImageView iv_status;
        private HorizontalScrollView hsv_goods_list;
        private LinearLayout Layout_List;
        private TextView tv_collocation_name, tv_save_text;

        private ParentViewHolder(View view) {
            v_space = view.findViewById(R.id.v_space);
            hsv_goods_list = (HorizontalScrollView) view.findViewById(R.id.hsv_goods_list);
            tv_collocation_name = (TextView) view.findViewById(R.id.tv_collocation_name);
            Layout_List = view.findViewById(R.id.Layout_List);
        }
    }

    private class ChildViewHolder {
        private ImageView sdv_goods_img;
        private LinearLayout ll_root_view;
        private TextView tv_goods_title;
        private TextView tv_googs_id;
        private TextView tv_goods_price;
        private TextView tv_googs_sales;
        private TextView tv_googs_money;

        private ChildViewHolder(View view) {
            sdv_goods_img = (ImageView) view.findViewById(R.id.sdv_goods_img);
            ll_root_view = (LinearLayout) view.findViewById(R.id.ll_root_view);
            tv_goods_title = (TextView) view.findViewById(R.id.tv_goods_title);
            tv_googs_id = (TextView) view.findViewById(R.id.tv_googs_id);
            tv_goods_price = (TextView) view.findViewById(R.id.tv_goods_price);
            tv_googs_sales = (TextView) view.findViewById(R.id.tv_googs_sales);
            tv_googs_money = (TextView) view.findViewById(R.id.tv_googs_money);

        }
    }


}
